package com.txsj.scan.activity.allocationapply;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lvfq.pickerview.TimePickerView;
import com.lvfq.pickerview.view.WheelTime;
import com.txsj.scan.R;
import com.txsj.scan.activity.SelectEmployeeActivity;
import com.txsj.scan.activity.SelectWarehouseActivity;
import com.txsj.scan.base.BaseActivity;
import com.txsj.scan.bean.EmployeeBean;
import com.txsj.scan.bean.EventAllocationBean;
import com.txsj.scan.bean.WarehouseBean;
import com.txsj.scan.presenter.AllocationScreenPresenter;
import com.txsj.scan.utils.AppDateMgr;
import com.txsj.scan.utils.ToastUtils;
import com.txsj.scan.utils.Trace;
import com.txsj.scan.view.IAllocationScreenView;
import com.txsj.scan.widget.TSnackbarView;
import com.txsj.scan.widget.TitleBarView;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

public class AllocationScreenActivity extends BaseActivity<AllocationScreenPresenter> implements IAllocationScreenView, View.OnClickListener {
    public static final int IN_INVENTORY = 0x176;
    public static final int IN_EMPLOYEE = 0x175;
    public static final int IN_STATE = 0x174;

    @Override
    protected AllocationScreenPresenter providePresenter() {
        return new AllocationScreenPresenter(this);
    }

    @Override
    public Context context() {
        return this;
    }

    TitleBarView mTitle;
    LinearLayout lin_start_date, lin_end_date, lin_store, lin_cangku, lin_zhidanren, lin_state;
    TextView tv_start_date, tv_end_date, tv_store_name, tv_cangkuName, tv_zhidanren_name, tv_state;
    Button btn_reset, btn_confirm;


    private String employee_id, billState, warehouse_id;
    private EmployeeBean inEmployeeBean;
    private WarehouseBean inWarehouseBean;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_allocation_screen);

        mTitle = (TitleBarView) findViewById(R.id.titleBar);
        lin_start_date = (LinearLayout) findViewById(R.id.lin_start_date);
        lin_start_date.setOnClickListener(this);
        lin_end_date = (LinearLayout) findViewById(R.id.lin_end_date);
        lin_end_date.setOnClickListener(this);
        lin_store = (LinearLayout) findViewById(R.id.lin_store);
        lin_store.setOnClickListener(this);
        lin_cangku = (LinearLayout) findViewById(R.id.lin_cangku);
        lin_cangku.setOnClickListener(this);
        lin_zhidanren = (LinearLayout) findViewById(R.id.lin_zhidanren);
        lin_zhidanren.setOnClickListener(this);
        lin_state = (LinearLayout) findViewById(R.id.lin_state);
        lin_state.setOnClickListener(this);

        tv_start_date = (TextView) findViewById(R.id.tv_start_date);
        tv_end_date = (TextView) findViewById(R.id.tv_end_date);
        tv_store_name = (TextView) findViewById(R.id.tv_store_name);
        tv_cangkuName = (TextView) findViewById(R.id.tv_cangkuName);
        tv_zhidanren_name = (TextView) findViewById(R.id.tv_zhidanren_name);
        tv_state = (TextView) findViewById(R.id.tv_state);
        tv_start_date.setText(format.format(new Date()));
        btn_reset = (Button) findViewById(R.id.btn_reset);
        btn_reset.setOnClickListener(this);
        btn_confirm = (Button) findViewById(R.id.btn_confirm);
        btn_confirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_reset:
                tv_start_date.setText("");
                tv_end_date.setText("");
                tv_cangkuName.setText("");
                tv_zhidanren_name.setText("");
                tv_state.setText("");
                warehouse_id = "";
                employee_id = "";
                billState = "";
                break;
            case R.id.btn_confirm:

                if (TextUtils.isEmpty(tv_start_date.getText().toString().trim())) {
                    showErrorMsg("请选择开始时间");
                    return;
                }

                if (TextUtils.isEmpty(tv_end_date.getText().toString().trim())) {
                    showErrorMsg("请选择结束时间");
                    return;
                }

                HashMap<String, String> map = new HashMap<>();
                map.put("startDate", tv_start_date.getText().toString().trim());
                map.put("endDate", tv_end_date.getText().toString().trim());
                if (!TextUtils.isEmpty(warehouse_id))
                    map.put("warehouse", warehouse_id);
                if (!TextUtils.isEmpty(employee_id))
                    map.put("employee", employee_id);
                if (!TextUtils.isEmpty(tv_state.getText().toString().trim())) {
                    map.put("stateName", tv_state.getText().toString());
                    map.put("state", billState);
                }
                EventAllocationBean bean = new EventAllocationBean(new Gson().toJson(map));
                EventBus.getDefault().post(bean);
                finish();
                break;
            case R.id.lin_start_date:
                selectDate(true);
                break;
            case R.id.lin_end_date:
                selectDate(false);
                break;
//            case R.id.lin_store:
//                break;
            case R.id.lin_cangku:
                Intent intent = new Intent(AllocationScreenActivity.this, SelectWarehouseActivity.class);
                startActivityForResult(intent, IN_INVENTORY);
                break;
            case R.id.lin_zhidanren:
                Intent intentEmployee = new Intent(AllocationScreenActivity.this, SelectEmployeeActivity.class);
                startActivityForResult(intentEmployee, IN_EMPLOYEE);
                break;
            case R.id.lin_state:
                Intent intentState = new Intent(AllocationScreenActivity.this, AllocationStateListActivity.class);
                startActivityForResult(intentState, IN_STATE);
                break;
        }
    }


    /**
     * 日期选择窗口
     */
    TimePickerView pvTime;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    private void selectDate(final boolean isStartDate) {
        pvTime = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        pvTime.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, WheelTime wheelTime) {
                try {
                    if (!isStartDate) {
                        //设置结束日期
                        if (AppDateMgr.isBeforeDate(format.format(new Date()), format.format(date))) {
                            ToastUtils.t(AllocationScreenActivity.this, "结束日期不能晚于今天");
                        } else {
                            if (!TextUtils.isEmpty(tv_start_date.getText().toString())) {
                                if (AppDateMgr.isBeforeDate(format.format(date), tv_start_date.getText().toString())) {
                                    ToastUtils.t(AllocationScreenActivity.this, "结束日期不能早于开始日期");
                                } else {
                                    tv_end_date.setText(format.format(date));
                                }
                            } else {
                                tv_end_date.setText(format.format(date));
                            }
                        }
                    } else {
                        //设置开始日期
                        if (AppDateMgr.isBeforeDate(format.format(new Date()), format.format(date))) {
                            ToastUtils.t(AllocationScreenActivity.this, "开始日期不能晚于今天");
                        } else {
                            if (!TextUtils.isEmpty(tv_end_date.getText().toString())) {
                                if (AppDateMgr.isBeforeDate(tv_end_date.getText().toString(), format.format(date))) {
                                    ToastUtils.t(AllocationScreenActivity.this, "结束日期不能早于开始日期");
                                } else {
                                    tv_start_date.setText(format.format(date));
                                }
                            } else {
                                tv_start_date.setText(format.format(date));
                            }
                        }
                    }
                } catch (Exception e) {

                    Trace.e("选择日期异常： " + new Gson().toJson(e));
                }
            }
        });
        pvTime.setCyclic(false);
        pvTime.setTextSize(14);
        pvTime.setTitle("请选择时间");
        pvTime.show();
    }

    @Override
    public void showLoading() {
        super.showLoading();
    }

    public void showErrorMsg(String msg) {
        new TSnackbarView(mTitle, msg);
    }

    @Override
    public void loadFailed(String errMsg) {
        showErrorMsg(errMsg);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (null == data || TextUtils.isEmpty(data.getStringExtra("result"))) {
            return;
        }
        String json = (String) data.getSerializableExtra("result");
        if (requestCode == IN_INVENTORY) {
            WarehouseBean bean = new Gson().fromJson(json, WarehouseBean.class);
            inWarehouseBean = bean;
            tv_cangkuName.setText(inWarehouseBean.getWarehouse_name());
        } else if (requestCode == IN_EMPLOYEE) {
            EmployeeBean bean = new Gson().fromJson(json, EmployeeBean.class);
            inEmployeeBean = bean;
            tv_zhidanren_name.setText(inEmployeeBean.getEmployee_name());
        } else if (requestCode == IN_STATE) {
            try {
                JSONObject jsonObject = new JSONObject(json);
                tv_state.setText(jsonObject.getString("stateName"));
                billState = jsonObject.getString("state");
            } catch (Exception e) {

            }
        }
    }
}
